package com.qire.manhua.activity;

import android.app.Activity;
import android.os.Bundle;

import com.qire.antscore.annotation.PropertyObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.antsrouter.lifecycleHandler.ActivityManageHandler;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.ObserveBinderHelper;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.ComicCommentController;
import com.qire.manhua.databinding.ActivityComicCommentBinding;
import com.qire.manhua.dialog.MessageEnablePromptDialog;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.IComicCommentViewModel;

import androidx.annotation.Nullable;

/**
 * 漫画评论页
 */
@RouteMapping(path = Navigation.Page.ComicCommentPage)
public class ComicCommentActivity extends BaseActivity<ActivityComicCommentBinding> {

    @Autowired
    ComicBookEntity comicBookEntity;
    @Autowired
    int chapterId = 0;

    @Autowired(injector = ViewModelInjector.class)
    ProxyViewModel<IComicCommentViewModel> viewModel;

    @Autowired(injector = EventControllerInjector.class)
    ComicCommentController controller;

    @Override
    public int layoutRid() {
        return R.layout.activity_comic_comment;
    }

    public int comicId() {
        return comicBookEntity.id;
    }

    public int chapterId() {
        return chapterId;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        IComicCommentViewModel viewModelProxy = viewModel.proxy();
        viewModelProxy.setTitle(comicBookEntity.bookName);
        viewModelProxy.setWordCount("(0/800)");

        controller.onAddition(viewModelProxy);
    }

    @Override
    protected void registerObserver() {
        super.registerObserver();
        ObserveBinderHelper.callObserveBindOfPropertyObserveBinder(viewModel, this);
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.COMMENT_SUBMIT_SUCCESS, event -> {
            ToastHelper.showSnackBarAtPreviousPage("评论成功", Prompt.SUCCESS);
            Activity previousActivity = ActivityManageHandler.HANDLER.previousActivity();
            MessageEnablePromptDialog.showIfNeed(previousActivity);
            finish();
            return true;
        });
    }

    @Override
    protected void unregisterObserver() {
        super.unregisterObserver();
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.COMMENT_SUBMIT_SUCCESS);
    }

    @PropertyObserve(name = "content")
    public void onContentChange(String content) {
        int wordCount = StringUtil.isEmpty(content) ? 0 : content.length();
        viewModel.proxy().setWordCount("(" + wordCount + "/800)");
    }

}
